package com.acwing.partition19;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author `RKC`
 * @date 2022/4/20 8:50
 */
public class AC1874哞加密 {

    private static final int N = 55;
    private static final int[][] dirs = {{-1, -1}, {-1, 0}, {1, 0}, {1, 1}, {0, -1}, {1, -1}, {-1, 1}, {0, 1}};
    private static String[] s = new String[N];
    private static Map<String, Integer> map = new HashMap<>();
    private static int n, m;

    private static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    private static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = nextInt();
        m = nextInt();
        for (int i = 0; i < n; i++) s[i] = next();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (s[i].charAt(j) == 'M') continue;
                for (int[] dir : dirs) {
                    StringBuilder sb = new StringBuilder(String.valueOf(s[i].charAt(j)));
                    int nx = i, ny = j;
                    boolean flag = true;
                    for (int k = 0; k < 2; k++) {
                        nx += dir[0];
                        ny += dir[1];
                        if (nx < 0 || nx >= n || ny < 0 || ny >= m) {
                            flag = false;
                            break;
                        }
                        sb.append(s[nx].charAt(ny));
                    }
                    if (flag && sb.charAt(0) != sb.charAt(1) && sb.charAt(1) == sb.charAt(2) && sb.charAt(1) != 'O') {
                        map.put(sb.toString(), map.getOrDefault(sb.toString(), 0) + 1);
                    }
                }
            }
        }
        int ans = 0;
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            ans = Math.max(ans, entry.getValue());
        }
        writer.println(ans);
        writer.flush();
    }

    private static String next() throws IOException {
        tokenizer.nextToken();
        return tokenizer.sval;
    }

    private static int nextInt() throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }
}
